//
//  BadgeView.m
//  etionUI
//
//  Created by wjxfhxy on 14-9-21.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "XUIBadgeView.h"


CGFloat const BadgeViewPointStyleWidthHeight = 10;

CGFloat const BadgeViewPointStyleCornerRadius = BadgeViewPointStyleWidthHeight / 2 - 1;

CGFloat const BadgeViewTextStyleHeight = 20;

CGFloat const BadgeViewTextStyleCornerRadius = BadgeViewTextStyleHeight / 2 - 1;

CGFloat const BadgeViewTextFontSize = 11;



@interface XUIBadgeView ()
{
    UILabel *_stringLabel;
}

@property(nonatomic,assign) XUIBadgeViewType badgeViewType;

@end

@implementation XUIBadgeView

@synthesize szTag;

- (id)initWithBadgeViewType:(XUIBadgeViewType)badgeViewType
{
    self = [super initWithFrame:CGRectZero];
    if (self)
    {
        self.userInteractionEnabled = NO;
        
        self.backgroundColor = [UIColor clearColor];
        
        self.badgeViewType = badgeViewType;
        
        self.bgColor = [UIColor redColor];
    }
    return self;
}

-(void)dealloc
{
    [_text release_mm];
    [_stringLabel release_mm];
    self.szTag = nil;
    [_bgColor release_mm];
    superdealloc_mm;
}

-(void)setBadgeViewType:(XUIBadgeViewType)badgeViewType
{
    _badgeViewType = badgeViewType;
    
    switch(_badgeViewType)
    {
        case XUIBadgeViewTypeByPointStyle:
        {
            self.size = CGSizeMake(BadgeViewPointStyleWidthHeight, BadgeViewPointStyleWidthHeight);
            
            break;
        }
        case XUIBadgeViewTypeByTextStyle:
        {
            self.height = BadgeViewTextStyleHeight;
            
            _stringLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, BadgeViewTextStyleCornerRadius * 2, BadgeViewTextStyleHeight)];
            _stringLabel.backgroundColor = [UIColor clearColor];
            _stringLabel.textColor = [UIColor whiteColor];
            _stringLabel.font = [UIFont systemFontOfSize:BadgeViewTextFontSize];
            _stringLabel.textAlignment = NSTextAlignmentCenter;
            
            [self addSubview:_stringLabel];

            break;
        }
        default:
        {
            break;
        }
    }
}

-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    switch(_badgeViewType)
    {
        case XUIBadgeViewTypeByPointStyle:
        {
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path addArcWithCenter:CGPointMake(rect.size.width/2, rect.size.height/2) radius:BadgeViewPointStyleCornerRadius startAngle:0.0 endAngle:M_PI * 2 clockwise:YES];
            [self.bgColor setFill];
            [path fill];
            break;
        }
        case XUIBadgeViewTypeByTextStyle:
        {
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(1, 1, MAX(rect.size.width - 1 - 1,BadgeViewTextStyleCornerRadius *2), BadgeViewTextStyleCornerRadius *2) cornerRadius:BadgeViewTextStyleCornerRadius];
            [self.bgColor setFill];
            [path fill];
            break;
        }
        default:
        {
            break;
        }
    }
}

-(void)setText:(NSString *)text
{
    if(_badgeViewType == XUIBadgeViewTypeByPointStyle)
        return;
    
    NSString *tmpText = [text retain_mm];
    [_text release_mm];
    _text = tmpText;
    
    CGSize s = [text stringSizeWithFont:_stringLabel.font];
    _stringLabel.text = text;
    _stringLabel.width = s.width;
    
    self.width = MAX(_stringLabel.width + BadgeViewTextStyleCornerRadius,BadgeViewTextStyleHeight);
    _stringLabel.centerX = self.width /2;
    
    [self setNeedsLayout];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGPoint centerPoint = CGPointMake(0, 0);
    
    switch(_badgeViewAlignment)
    {
        case XUIBadgeViewAlignmentByTopRight:
        {
            centerPoint = CGPointMake(self.superview.width,0);
            break;
        }
        case XUIBadgeViewAlignmentByCenterRight:
        {
            centerPoint = CGPointMake(self.superview.width,self.superview.height /2);
            break;
        }
        case XUIBadgeViewAlignmentByCenterLeft:
        {
            centerPoint = CGPointMake(0,self.superview.height /2);
            break;
        }
        case XUIBadgeViewAlignmentByTopCenter:
        {
            centerPoint = CGPointMake(self.superview.width /2,0);
            break;
        }
        default:
        {
            centerPoint = self.center;
            break;
        }
    }
    
    centerPoint = CGPointMake(centerPoint.x + _badgeViewCenterOffset.x, centerPoint.y + _badgeViewCenterOffset.y);
    
    self.center = centerPoint;
}

-(void)setBadgeViewCenterOffset:(CGPoint)badgeViewCenterOffset
{
    _badgeViewCenterOffset = badgeViewCenterOffset;
    
    [self setNeedsLayout];
}

+(XUIBadgeView*)badgeViewWithBadgeViewType:(XUIBadgeViewType)badgeViewType andParentView:(UIView*)parentView byAlignment:(XUIBadgeViewAlignment)alignment
{
    XUIBadgeView *badgeView = [[[XUIBadgeView alloc]initWithBadgeViewType:badgeViewType] autorelease_mm];
    [parentView addSubview:badgeView];
    badgeView.badgeViewAlignment = alignment;
    return badgeView;
}

+(XUIBadgeView*)badgeViewWithBadgeViewType:(XUIBadgeViewType)badgeViewType andParentView:(UIView*)parentView
{
    return [XUIBadgeView badgeViewWithBadgeViewType:badgeViewType andParentView:parentView byAlignment:XUIBadgeViewAlignmentByAny];
}

@end
